<template>
	<view>
		<view class="my-settle">
			<label class="radio"><radio value="r1" :checked="allSelect" @click="isAll" />全选</label>
			<view class="price">
				合计: <text>￥ {{ allPrice }}</text>
			</view>
			<view class="settle" @click="settle">结算 ({{ allCount }})</view>
		</view>
	</view>
</template>

<script>
	import { createOrder } from '@/api/index.js'
	import { mapGetters, mapState } from 'vuex'
	export default {
		name:"my-settle",
		data() {
			return {
				time: 3,
				timer: null
			};
		},
		computed: {
			...mapGetters(['allCount', 'allSelect', 'allPrice']),
			token() {
				return this.$store.state.user.token
			},
			address() {
				return this.$store.state.user.address
			}
		},
		methods: {
			// 点击全选
			isAll(e) {
				this.$store.commit('cart/updataAllStatus', !this.allSelect)
			},
			settle() {
				// 购物车没有选中商品
				if (!this.allCount) return uni.showToast({
					title: '请选择要购买的商品',
					icon: 'none'
				})
				// 没有选中地址
				if (!this.address.userName) return uni.showToast({
					title: '请选择收货地址',
					icon: 'none'
				})
				// 未登录跳转登录页
				if (!this.token) {
					this.time = 3
					this.showTips()
					this.timer = setInterval(() => {
						this.time--
						if (this.time <= 0) {
							clearInterval(this.timer)
							this.time = 3
							return uni.switchTab({
								url: '/pages/my/my'
							})
						}
						this.showTips()
					}, 1000)
					return
				}
				// 支付
				this.pay()
			},
			async pay() {
				const res = await createOrder({
					order_price: 0.01,
					consignee_addr: this.$store.state.user.address,
					goods: this.$store.state.cart.cart.filter(x => x.goods_state).map(item =>  {
						return {goods_id: item.goods_id, goods_number: item.goods_count, goods_price: item.goods_price}
					})
				})
				console.log(res);
				uni.showToast({
					title: '想付钱，没门',
					icon: 'exception'
				})
			},
			// 倒计时提示
			showTips() {
				uni.showToast({
					title: `${this.time}秒后跳到登录页`,
					icon: 'none',
					mask: true,
					duration: 1500
				})
			}
		}
	}
</script>

<style scoped lang="scss">
.my-settle {
	display: flex;
	justify-content: space-between;
	background-color: #fff;
	padding-left: 20rpx;
	align-items: center;
	.price {
		text {
			color: #f00;
			font-size: 32rpx;
		}
	}
	.settle {
		width: 200rpx;
		height: 100rpx;
		background-color: #f00;
		text-align: center;
		line-height: 100rpx;
		color: #fff;
	}
}
</style>
